﻿using System.Windows.Forms;
using System.IO;

namespace Grinder
{
    public sealed partial class RangeForm : Form
    {
        public string SelectedRange
        {
            get
            {
                return string.Format("[substr:{0},{1}]", textFileName.SelectionStart, (textFileName.SelectionLength + textFileName.SelectionStart));
            }
        }

        public DialogResult ShowDialog(string file)
        {
            var fileInfo = new FileInfo(file);
            if (!string.IsNullOrEmpty(fileInfo.Extension))
                file = file.Replace(fileInfo.Extension, "");

            textFileName.Text = file;
            textFileName.SelectionStart = 0;
            textFileName.SelectionLength = textFileName.Text.Length;
            
            lblToken.Text = "";

            return ShowDialog();
        }

        public RangeForm()
        {
            InitializeComponent();            
        }

        private void textFileName_MouseUp(object sender, MouseEventArgs e)
        {
            UpdateTokenLabel();
        }

        private void textFileName_KeyUp(object sender, KeyEventArgs e)
        {
            UpdateTokenLabel();
        }

        private void UpdateTokenLabel()
        {
            lblToken.Text = textFileName.SelectionLength != 0 ? SelectedRange : "";
        }        
    }
}
